<template>
  <transition
    enter-active-class="animated fadeInUp"
    leave-active-class="animated fadeOutDown"
  >
    <div class="setting-wrapper" v-show="menuVisible && settingVisible === 2">
      <div class="setting-progress">
        <div class="read-time-wrapper">
          <span class="read-time-text">{{getReadtimeText(this.fileName)}}</span>
          <span class="iconfont">&#xe743;</span>
        </div>
        <div class="progress-wrapper">
          <div class="progress-icon-wrapper" @click="prevSection()">
            <span class="iconfont">&#xe744;</span>
          </div>
          <input
            type="range"
            class="progress"
            max="100"
            min="0"
            step="1"
            @change="onProgressChange($event.target.value)"
            @input="onProgressInput($event.target.value)"
            :value="progress"
            :disabled="!bookAvailable"
            ref="progress"
          >
          <div class="progress-icon-wrapper" @click="nextSection()">
            <span class="iconfont">&#xe743;</span>
          </div>
        </div>
        <div class="text-wrapper">
          <span class="progress-section-text">{{getSectionName}}</span>
          <span>({{bookAvailable ? progress + '%' : '加载中...'}})</span>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { ebookMixin } from '../../utils/mixin'

export default {
  name: 'EbookSettingProgress',
  mixins: [ebookMixin],
  methods: {
    onProgressChange (progress) {
      this.setProgress(progress).then(() => {
        this.displayProgress()
        this.updateProgressBg()
      })
    },
    displayProgress () {
      // 参数为小数，获取当前定位
      const cfi = this.currentBook.locations.cfiFromPercentage(this.progress / 100)
      // 渲染所定位到的页面
      this.display(cfi)
    },
    onProgressInput (progress) {
      // 拖动的同时进度条发生变化，如果想拖动的同时页面发生变化则添加回调函数即可
      this.setProgress(progress).then(() => {
        this.updateProgressBg()
      })
      this.updateProgressBg()
    },
    updateProgressBg () {
      this.$refs.progress.style.backgroundSize = `${this.progress}% 100%`
    },
    prevSection () {
      // 如果等于0再返回上一章，则会溢出
      if (this.section > 0 && this.bookAvailable) {
        this.setSection(this.section - 1).then(() => {
          this.disPlaySection()
        })
      }
    },
    nextSection () {
      if (this.section < this.currentBook.spine.length - 1 && this.bookAvailable) {
        this.setSection(this.section + 1).then(() => {
          this.disPlaySection()
        })
      }
    },
    disPlaySection () {
      const sectionInfo = this.currentBook.section(this.section)
      if (sectionInfo && sectionInfo.href) {
        this.display(sectionInfo.href)
      }
    }
  },
  updated () {
    this.updateProgressBg()
  }
}
</script>

<style lang="stylus" scoped>
  @import '../../assets/styles/global.styl'
  .setting-wrapper
    background: white
    position: absolute
    bottom: px2rem(48)
    left: 0
    z-index: 101
    width: 100%
    height: px2rem(90)
    box-shadow: 0 px2rem(-8) px2rem(8) rgba(0, 0, 0, .15)
  .setting-progress
    position: relative
    width: 100%
    height: 100%
    .read-time-wrapper
      position: absolute
      left: 0
      top: 0
      width: 100%
      height: px2rem(40)
      font-size: px2rem(12)
      center()
    .text-wrapper
      center()
      position: absolute
      left: 0
      bottom: px2rem(10)
      width: 100%
      color: #333
      font-size: px2rem(12)
      padding: 0 px2rem(15)
      box-sizing: border-box
      .progress-section-text
        // 超出范围使用省略号
        ellipsis()
    .progress-wrapper
      width: 100%
      height: 100%
      center()
      padding: 0 px2rem(15)
      box-sizing: border-box
      .progress-icon-wrapper
        .iconfont
          font-size: px2rem(24)
      .progress
        width: 100%
        // 覆盖默认样式
        -webkit-appearance: none
        height: px2rem(2)
        margin: 0 px2rem(10)
      .progress:focus
        outline: none
      .progress::-webkit-slider-thumb
        -webkit-appearance: none
        height: px2rem(20)
        width: px2rem(20)
        border-radius: 50%
        background: white
        box-shadow: 0 4px 4px rgba(0, 0, 0, .15)
        border: px2rem(1) solid #ddd
</style>
